package Test;

public class test01 {
    public static final Object locker = new Object();
    public static Integer i = 1;
    public static int end = 100;
    public static class myThread extends Thread{
        @Override
        public void run() {
            synchronized(locker){
                while(i<=end){
                    System.out.println(Thread.currentThread().getName() +" "+ i);
                    i++;
                    //唤醒别的线程:
                    locker.notifyAll();
                    try {
                        //让当前线程进入阻塞状态:
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        }
    }
    public static void main(String[] args) {
        //使用两个线程交替打印1-100
        Thread thread1 = new Thread(new myThread());
        Thread thread2 = new Thread(new myThread());
        thread1.start();
        thread2.start();
    }
}
